package com.taomus.mytools.other.mail;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * rcmu@travelsky.com
 * mrc123$567*
 * @author rcmu
 *
 */
public class MyMail {

	private STMP stmp;
	private String from;
	private String to;
	private String cc;
	private String bcc;
	private String userName;
	private String userPwd;
	private List<File> enclosure;

	public MyMail(STMP stmp) {
		this.stmp = stmp;
	}

	public STMP getStmp() {
		return stmp;
	}

	public void setStmp(STMP stmp) {
		this.stmp = stmp;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	private List<MimeBodyPart> addEnclosure(List<File> files) throws Exception {
		List<MimeBodyPart> mbplist = null;
		if (files != null) {
			mbplist = new ArrayList<>();
			for (File file : files) {
				// 9. 创建附件"节点"
				MimeBodyPart attachment = new MimeBodyPart();
				// 读取本地文件
				DataHandler dh2 = new DataHandler(new FileDataSource(file));
				// 将附件数据添加到"节点"
				attachment.setDataHandler(dh2);
				// 设置附件的文件名（需要编码）
				attachment.setFileName(MimeUtility.encodeText(dh2.getName()));
			}
		}
		return mbplist;
	}

	public void send(String subject, String text) throws Exception {
		Properties properties = new Properties();
		if (stmp != null) {
			properties.put("mail.transport.protocol", "smtp");// 连接协议
			properties.put("mail.smtp.host", this.stmp.getHost());// 主机名
			properties.put("mail.smtp.port", this.stmp.getProt());// 端口号
		}
		// 得到回话对象
		Session session = Session.getInstance(properties);
		session.setDebug(true);
		// 获取邮件对象
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(this.from));
		setContacts2Mail(message, Message.RecipientType.TO, this.to);
		setContacts2Mail(message, Message.RecipientType.CC, this.cc);
		setContacts2Mail(message, Message.RecipientType.BCC, this.bcc);
		// 设置收件人邮箱地址
		message.setSubject(subject);
		if (this.enclosure != null) {
			MimeBodyPart mbptext = new MimeBodyPart();
			mbptext.setContent(text, "text/html;charset=UTF-8");
			MimeMultipart mm = new MimeMultipart();
			mm.addBodyPart(mbptext);
			List<MimeBodyPart> mbplist = this.addEnclosure(this.enclosure);
			for (MimeBodyPart attachment : mbplist) {
				mm.addBodyPart(attachment); // 如果有多个附件，可以创建多个多次添加
			}
			mm.setSubType("mixed"); // 混合关系
			message.setContent(mm);
		} else {
			// 设置邮件内容
			message.setText(text);
		}

		// 得到邮差对象
		Transport transport = session.getTransport();
		// 连接自己的邮箱账户
		transport.connect(this.userName, this.userPwd);// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
		// 发送邮件
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}

	private void setContacts2Mail(Message message, RecipientType rt, String contacts)
			throws AddressException, MessagingException {
		Set<InternetAddress> toSet = new HashSet<>();
		if (contacts != null) {
			String[] tos = contacts.split(";");
			for (String to : tos) {
				if (to.contains("@")) {
					toSet.add(new InternetAddress(to));
				}
			}
			message.setRecipients(rt, (InternetAddress[]) toSet.toArray());
		}
	}

	public List<File> getEnclosure() {
		return this.enclosure;
	}

	public void setEnclosure(List<File> enclosure) {
		this.enclosure = enclosure;
	}
}
